package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.LogUtils;
import com.ruoyi.system.domain.LatitudeAndLongitude;
import com.ruoyi.system.mapper.LatitudeAndLongitudeMapper;
import org.gavaghan.geodesy.Ellipsoid;
import org.gavaghan.geodesy.GlobalCoordinates;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TourismScenicSpotMapper;
import com.ruoyi.system.domain.TourismScenicSpot;
import com.ruoyi.system.service.ITourismScenicSpotService;

import javax.annotation.Resource;

/**
 * 景区Service业务层处理
 *
 * @author gqd
 * @date 2020-12-25
 */
@Service
public class TourismScenicSpotServiceImpl implements ITourismScenicSpotService
{
    @Autowired
    private TourismScenicSpotMapper tourismScenicSpotMapper;
    @Resource
    private LatitudeAndLongitudeMapper latitudeAndLongitudeMapper;

    /**
     * 查询景区
     *
     * @param id 景区ID
     * @return 景区
     */
    @Override
    public TourismScenicSpot selectTourismScenicSpotById(Integer id)
    {
        return tourismScenicSpotMapper.selectTourismScenicSpotById(id);
    }

    /**
     * 查询景区列表
     *
     * @param tourismScenicSpot 景区
     * @return 景区
     */
    @Override
    public List<TourismScenicSpot> selectTourismScenicSpotList(TourismScenicSpot tourismScenicSpot)
    {
        return tourismScenicSpotMapper.selectTourismScenicSpotList(tourismScenicSpot);
    }

    /**
     * 新增景区
     *
     * @param tourismScenicSpot 景区
     * @return 结果
     */
    @Override
    public int insertTourismScenicSpot(TourismScenicSpot tourismScenicSpot)
    {
        tourismScenicSpot.setCreateTime(DateUtils.getNowDate());
        return tourismScenicSpotMapper.insertTourismScenicSpot(tourismScenicSpot);
    }

    /**
     * 修改景区
     *
     * @param tourismScenicSpot 景区
     * @return 结果
     */
    @Override
    public int updateTourismScenicSpot(TourismScenicSpot tourismScenicSpot)
    {
        tourismScenicSpot.setUpdateTime(DateUtils.getNowDate());
        return tourismScenicSpotMapper.updateTourismScenicSpot(tourismScenicSpot);
    }

    /**
     * 批量删除景区
     *
     * @param ids 需要删除的景区ID
     * @return 结果
     */
    @Override
    public int deleteTourismScenicSpotByIds(Integer[] ids)
    {
        return tourismScenicSpotMapper.deleteTourismScenicSpotByIds(ids);
    }

    /**
     * 删除景区信息
     *
     * @param id 景区ID
     * @return 结果
     */
    @Override
    public int deleteTourismScenicSpotById(Integer id)
    {
        return tourismScenicSpotMapper.deleteTourismScenicSpotById(id);
    }

    /**
     * 查询景区的信息
     * @return
     */
    @Override
    public AjaxResult selectScenicSpotAll() {
        TourismScenicSpot tourismScenicSpot = tourismScenicSpotMapper.selectScenicSpotAll();
        if(tourismScenicSpot == null){
            return AjaxResult.error("暂时没有景区的有关信息！");
        }
        LatitudeAndLongitude latitudeAndLongitude = latitudeAndLongitudeMapper.selectIt();
        GlobalCoordinates source = new GlobalCoordinates(Double.parseDouble(latitudeAndLongitude.getLatone()), Double.parseDouble(latitudeAndLongitude.getLngone()));
        GlobalCoordinates target = new GlobalCoordinates(Double.parseDouble(latitudeAndLongitude.getLattwo()), Double.parseDouble(latitudeAndLongitude.getLngtwo()));
        double meter2 = LogUtils.getDistanceMeter(source, target, Ellipsoid.WGS84);
        int i = new Double(meter2).intValue()/1000;
        tourismScenicSpot.setDistance(i);
        return AjaxResult.success(tourismScenicSpot);
    }
}
